/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package supermarketv1;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import supermarketv0_class.*;

/**
 *
 * @author ΠΑΝΑΓΙΩΤΗΣ v1
 */
public class MakingPurchases extends javax.swing.JFrame {

    Customer customer;
    Store store;
    List stores;
    Product product=new Product();
    List products;
    ArrayList<Product> selectedpro;
    SuperMarketMainView1 mainscreen;
    ArrayList<Purchase> lastpurchase;
    ArrayList selestedquantity;
    int sumpoints=0;
    int quantity ;
    int total=0;

    /**
     * Creates new form MakingPurchases
     */
    public MakingPurchases(SuperMarketMainView1 mainscreen,Customer cust) {
        initComponents();
        this.mainscreen = mainscreen;
        this.customer=cust;
        selectedpro= new ArrayList();
        lastpurchase= new ArrayList();
        selestedquantity=new ArrayList();
        stores = mainscreen.db.readStores();//ανάγνωση Καταστημάτων & Προϊοντων
        products = mainscreen.db.readProduct();
        ArrayList selestedquantity=new ArrayList();
        jComboBox1.removeAllItems();//ΚΑΘΑΡΙΣΜΟΣ
        jComboBox1.addItem("Select Store");
        Iterator itr = stores.iterator();
        while (itr.hasNext()) {//ΔΙΑΤΡΕΧΩ ΤΗ ΛΙΣΤΑ
            Store s = (Store) itr.next();
            jComboBox1.addItem(s);//ΠΡΟΣΘΗΚΗ ΣΤΟ  combobox
        }

        if (stores.isEmpty()) {//απενεργοποίηση πίνακα και κουμπιών
            jTable1.disable();
            jButton2.setEnabled(false);
            jButton1.setEnabled(false);

        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(supermarketv1.SuperMarketv1App.class).getContext().getResourceMap(MakingPurchases.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTable1);

        jButton1.setBackground(resourceMap.getColor("jButton1.background")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setBackground(resourceMap.getColor("jButton3.background")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(23, 23, 23)
                                .addComponent(jButton3))
                            .addComponent(jButton1)
                            .addComponent(jButton2))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(jLabel1)
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(49, 49, 49)
                        .addComponent(jButton2)
                        .addGap(74, 74, 74)
                        .addComponent(jButton3))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:Επιλογή καταστήματος
       
        if (jComboBox1.getSelectedIndex()> 0) { //αν έχω επιλέξει κατάστημα
            jTable1.removeAll();
            String[] columnNames = {"Όνομα", "Πόντοι", "Τιμή"};//τίτλοι των στηλών του πίνακα
            Object[][] data = new Object[products.size()][3];//εδώ θα μπουν οι γραμμές του πίνακα
            int i=0;
            store = (Store) jComboBox1.getSelectedItem();
            Iterator itr = products.iterator();//ΔΕΙΚΤΗΣ ΚΑΤΗΓΟΡΙΑ
            while (itr.hasNext()) {//ΔΙΑΤΡΕΧΩ ΤΗΝ ΛΙΣΤΑ
                
                Product p = (Product) itr.next();    //ΠΙΑΝΩ το Προϊόν
                
                Iterator itr_store = p.getStoreList().iterator();
                
              
                while (itr_store.hasNext()) {
                    Store s1 = (Store) itr_store.next();

                    if (s1.getStoreId() == store.getStoreId()) {
                       
                         System.out.println("fora1:"+i);
                             //ΟΙ ΣΤΗΛΕΣ ΤΟΥ ΠΙΝΑΚΑ
                            data[i][0] = p.getName();
                            data[i][1] = p.getPoints();
                            data[i][2] = p.getPrice();
                            

                            i++;
                           
                 } 
                    
                }
                
           
            }
            jTable1.setModel(new javax.swing.table.DefaultTableModel(data, columnNames));//δημιουργία πίνακα
            jTable1.repaint();//επανασχεδιασμός πίνακα
        }
       

    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:Προσθήκη στο Καλάθι
         int selectedproduct= jTable1.getSelectedRow();
      
         if(selectedproduct>0){
             
             
             
             //selectedpro.set(selectedproduct, product);
            
             String tmp= JOptionPane.showInputDialog(null,"Δώσε Ποσότητα", JOptionPane.INPUT_VALUE_PROPERTY, JOptionPane.YES_OPTION);
             quantity=Integer.parseInt(tmp);
          product= (Product)products.get(selectedproduct);
                selectedpro.add(product);
             selestedquantity.add(quantity);
           
            
             //selectedpro.containsAll(product.getStoreList());
            
         
          
         
          
        
           
             
         }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:Μετάβαση στο καλάθι
       
         mainscreen.shopcart=new ShoppingCart(mainscreen,customer,selectedpro,selestedquantity,store);
         mainscreen.shopcart.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         mainscreen.shopcart.setVisible(true); //show the form;
        
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:Κλείσιμο
        dispose();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jTable1MouseClicked
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
